/************************************************************************/
/* Resources                                                            */
/************************************************************************/
#include "Resources/Images/SDLImage.h"

namespace TD_Resources {

	/************************************************************************/
	/* Private functions                                                    */
	/************************************************************************/
	/**Should throw some exception, instead of a boolean value!*/
	bool SDLImage::optimizeImg() {
		SDL_Surface* optimizedImage = NULL;
		if(m_pImage != NULL) { 
			optimizedImage = SDL_DisplayFormat(m_pImage);
			if(optimizedImage != NULL) {
				SDL_FreeSurface(m_pImage);
				m_pImage = optimizedImage;
				return true;
			}
			return false;
		}
		return false;
	}


	/************************************************************************/
	/* Protected functions                                                  */
	/************************************************************************/
	void SDLImage::blitSurface(SDL_Surface * const g_pSurface, SDL_Rect *g_offsetRect, SDL_Rect * g_pClippingOffset /* = NULL*/) {
		//Clipping is the second parameter, with no clipping it is NULL
		SDL_BlitSurface(m_pImage, g_pClippingOffset, g_pSurface, g_offsetRect);
	}

	/************************************************************************/
	/* Constructors				                                            */
	/************************************************************************/
	SDLImage::SDLImage() : Resource () {
		m_pImage = NULL;
	}


	SDLImage::SDLImage(const SDLImage &rhs) {
		m_pImage = rhs.getSDLSurface();
	}


	SDLImage & SDLImage::operator=(const SDLImage &rhs) {
		m_pImage = rhs.getSDLSurface();
		return(*this);
	}

	SDLImage::SDLImage(const string &resourceName) : Resource (resourceName){
		m_pImage = NULL;
	}

	SDLImage::~SDLImage() {
		SDL_FreeSurface(m_pImage);
	}

	/************************************************************************/
	/* Functionalities                                                      */
	/************************************************************************/
	/**Should throw InvalidFile exception, instead of a boolean value!*/
	bool SDLImage::loadFile(const string &filename) {
		bool greatSuccess; 
		
		m_pImage = IMG_Load(filename.c_str());
		if(m_pImage == NULL) {
			#ifndef _WII
				throw ios_base::failure("Could not load the image file:" + filename);
			#endif
		}

		greatSuccess = optimizeImg();
		if(greatSuccess) {
			m_bounds.x = m_pImage->w;
			m_bounds.y = m_pImage->h;
		} else {
			#ifndef _WII
				throw bad_exception("Optmize Image error.");
			#endif
		}
		return(greatSuccess);
	}

	bool SDLImage::loadColorKeyFile(const string &file, const Color &keyColor) {
		bool greatSuccess; 
		m_pImage = IMG_Load(file.c_str()); 
		greatSuccess = optimizeImg();
		if(greatSuccess) {
			Uint32 colorkey = SDL_MapRGB(m_pImage->format, keyColor.red, keyColor.green, keyColor.blue);
			//Set all pixels of color 0xFF, 0, 0xFF to be transparent 
			SDL_SetColorKey(m_pImage, SDL_SRCCOLORKEY, colorkey ); 
			m_bounds.x = m_pImage->w;
			m_bounds.y = m_pImage->h;
		}
		return(greatSuccess);
	}

	void SDLImage::render(SDL_Surface *g_destSurface, unsigned int deltaTime) {

	}
	
}

